const express = require('express');

const app = express();

//写一个中间件就可以实现防盗链
app.use((req, res, next) => {
    let referer = req.get('referer');
    if (referer) {
        let url = new URL(referer);
        let hostname =  url.hostname;
        if (hostname !== '127.0.0.1') {
            res.status(404).send('<h1>404 Not Found</h1>');
            return;
        }
    }
    next();
});


//创建静态资源的中间件 无路径会访问index.html
app.use(express.static(__dirname + '/public'));

app.listen(3000,() => {
    console.log('服务已启动 3000端口正在监听....');
});